//
//  TeacherCheckOnList.m
//  iBaby365v2
//
//  Created by Kirinrin on 14/12/19.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "TeacherCheckOnListViewController.h"
#import <UIImageView+AFNetworking.h>
@implementation TeacherCheckOnListViewController
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    self.clearsSelectionOnViewWillAppear = YES;
}

#pragma mark - UITableDataDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.childArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSDictionary *childDict = self.childArray[indexPath.row];
    
    UIImageView *childPhoto = (UIImageView *)[cell viewWithTag:100];
    
    if (![childDict[@"photo"] isEqual:[NSNull null]]) {
        NSURL *photoUrl = [NSURL URLWithString:childDict[@"photo"]];
        [childPhoto setImageWithURL:photoUrl placeholderImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    }else{
        [childPhoto setImage:[UIImage imageNamed:@"imageUserPhotoDefault"]];
    }
    
    
    UITextField *name = (UITextField *)[cell viewWithTag:101];
    [name setText:childDict[@"name"]];
    
    UITextField *count = (UITextField *)[cell viewWithTag:102];
    [count setText:@"0"];
    [self.countArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[0] isEqualToString:childDict[@"id"]]) {
            [count setText:[NSString stringWithFormat:@"%@", obj[1]]];
            *stop = YES;
        }
    }];
    

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"toChildCheckOnCalendar" sender:indexPath];
}

#pragma mark - navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"toChildCheckOnCalendar"]) {
        NSIndexPath *indexpath = (NSIndexPath *)sender;
        [segue.destinationViewController setValue:self.childArray[indexpath.row] forKey:@"childDict"];
    }
}
@end
